﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WMSSyncData.Model.WMSModel;
using WMSSyncData.Utils;

namespace WMSSyncData.DAL.SqlDB
{
    public class WMS_T_InStorckRowDetailsDAL : BaseSqlDBDAL<WMS_T_InstockRowDetails>
    {
        public List<WMS_T_InstockRowDetails> GetAllUpdate()
        {
            string sql = @"SELECT * FROM dbo.wms_T_InStockRowDetails 
WHERE BillNo IN (SELECT BillNo FROM dbo.wms_T_InStock WHERE Status='1' and BillType<>'返工入库单')";
            var dt = SqlDBHelper.GetDataSet(sql).Tables[0];

            var result = MakeTablePackage(dt);
            return result;

        }

        public List<WMS_T_InstockRowDetails> GetSameBoxNoAndBillNo()
        {
            var sql = @"SELECT * FROM dbo.wms_T_InStockRowDetails WHERE BoxNo IN (

SELECT BoxNo FROM dbo.wms_T_InStockRowDetails WHERE ISNULL(BillNo,'')<>'' AND Status='0'   GROUP BY BoxNo HAVING COUNT(*)>1
) ORDER BY BoxNo";

            var dt = SqlDBHelper.GetDataSet(sql).Tables[0];

            var result = MakeTablePackage(dt);

            return result;
        }

        public bool UpdateSameBoxNoStatus(List<WMS_T_InstockRowDetails> list)
        {
            var result = false;
            if (list.Count() > 0)
            {
                var sqlWhere = "where guid in (";
                foreach (var item in list)
                {
                    sqlWhere = sqlWhere + "'" + item.Guid + "',";
                }
                sqlWhere = sqlWhere.TrimEnd(',');
                sqlWhere = sqlWhere + ")";
                var sql = "UPDATE  WMS_T_InstockRowDetails SET Status='H'" + sqlWhere;
                var updateRes = SqlDBHelper.ExcuteSQL(sql);
                if (updateRes > 0)
                {
                    result = true;
                }
            }
            return result;
        }
    }
}
